import { baseUrl } from "@/config/env";
import { getAnnexInfo } from "@/api/workFlow/WFLaunch";
export default {
 methods: {
  async flowTabsClick(activeName, oldActiveName){
    if(oldActiveName=='form'){//离开表单信息tab，需要提交数据
      await this.tabBeforeLeave(this.currentActiveName, this.currentActiveName);
      this.activeName = this.currentActiveName;
      this.currentActiveName = "";
    }
    if(activeName=='form'){//回到表单信息tab，重新显示
      this.currentActiveName=this.activeName;
    }
    //
    if(activeName=='files'){//附件汇总
      if( this.taskId&&this.processInstanceId){
        let res = await getAnnexInfo({
          taskId: this.taskId,
          processInstanceId: this.processInstanceId
        });
      if(res&&res.data&&res.data.success){
        this.processInfo.files = res.data.data;
      }
      }
    }
  },
  async tabBeforeLeave(activeName, oldActiveName) {
     this.currentActiveName = oldActiveName;
      let activeIndex = oldActiveName.split("-")[1];
      let vueRefComponent = this.$refs[this.currentActiveName];
      let value = vueRefComponent[0];
      let valueData = value._data;
      let formInfoIndex = valueData.formIndex;
      return new Promise((resolve, reject) => {
      value.submitForm().then((returnData) => {
        this.changeReturnData(activeIndex,formInfoIndex,valueData,returnData);
         this.currentActiveName = activeName;
        resolve()
      }).catch(err => {
        reject(err)
      })
    })
  },
  changeReturnData(activeIndex,formInfoIndex,valueData,returnData){
    if (returnData.code == 0) {
      // 0 提交失败
      this.formReturnData[activeIndex].formSubmitError = true;
      this.formReturnData[activeIndex].returnData = returnData;
      this.formInfoList[formInfoIndex].valid = false;
      return false;
    } else if (returnData.code == 1) {
      // 1 不需要提交
      this.formReturnData[activeIndex].formSubmitError = false;
      this.formReturnData[activeIndex].returnData = returnData;
      this.formInfoList[formInfoIndex].valid = true;
    } else {
      //2 提交数据
      let params = returnData.params;
      let forms_data = {};
      if (params) {
        if (params.F_FormId) {
          forms_data = params;
        } else {
          for (let k of Object.keys(params)) {
            let ParamValue = params[k];
            let splitK = k.split("Entity");
            let myKey = splitK.length > 0 ? splitK[0] : "";
            if (Array.isArray(ParamValue)) {
              forms_data[myKey] = ParamValue;
            } else {
              for (let filedKey of Object.keys(ParamValue)) {
                forms_data[filedKey] = params[k][filedKey];
              }
            }
          }
        }
      }
      let fileValue = returnData.fileValue;
      if(fileValue&&fileValue.length>0){
        this.formData.fileValue =[...new Set([...this.formData.fileValue, ...fileValue])] ;
      }
      this.processInfo.forms_data[activeIndex] = forms_data;
      this.formInfoList[formInfoIndex].valid = true;
      this.formReturnData[activeIndex].formSubmitError = false;
      this.formReturnData[activeIndex].returnData = returnData;
      if (valueData.formType == 0) {
        //自定义表单
        this.formIdParams[activeIndex] = {
          F_FormId: params.F_FormId,
          F_Type: 0,
          F_Url: ""
        };
        if (this.formPrimaryKey[activeIndex]) {
          params = { ...params, primaryKey: this.formPrimaryKey[activeIndex] };
          this.Ids[activeIndex] = this.formPrimaryKey[activeIndex];
          const promise = {
            url:
              baseUrl +
              `/form-scheme/${params.F_FormId}/instance-form-list/${this.formPrimaryKey[activeIndex]}`,
            method: "PUT",
            data: params
          };
          this.promiseList[activeIndex] = promise;
        } else {
          this.Ids[activeIndex] = "";
          const promise = {
            url:
              baseUrl + `/form-scheme/${params.F_FormId}/instance-form-list`,
            method: "POST",
            data: params
          };
          this.promiseList[activeIndex] = promise;
        }
      } else if (valueData.formType == 2) {
        let idKey = this.formPrimaryKey[activeIndex];
        this.Ids[activeIndex] = idKey;
        this.formIdParams[activeIndex] = {
          F_FormId: this.formInfoList[formInfoIndex].F_Id,
          F_Type: 2,
          F_Url: returnData.addressUrl
        };
        if (idKey) {
          let promiseParams = {
            ...params,
            F_url: returnData.addressUrl,
            idKey
          };
          const promise = {
            url: baseUrl + `/${returnData.addressUrl}/${idKey}`,
            method: "PUT",
            data: promiseParams
          };
          this.promiseList[activeIndex] = promise;
        } else {
          let promiseParams = { ...params, F_url: returnData.addressUrl };
          const promise = {
            url: baseUrl + `/${returnData.addressUrl}`,
            method: "POST",
            data: promiseParams
          };
          this.promiseList[activeIndex] = promise;
        }
      }
    }
    

  },
 },
};
